package com.file.xml.service.impl;

import com.file.xml.read.ReaderXml;
import com.file.xml.read.XmlBuilder;
import com.file.xml.pojo.Xml;
import com.file.xml.service.XmlDataService;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.List;

@Service
public class XmlDataServiceImpl implements XmlDataService {
 
    @Override
    public List<Xml> listXml() throws Exception {
        //读取Resource目录下的XML文件
        Resource resource = new ClassPathResource("XMLList.xml");
        //利用输入流获取XML文件内容
        BufferedReader br = new BufferedReader(new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8));
        StringBuffer buffer = new StringBuffer();
        String line = "";
        while ((line = br.readLine()) != null) {
            buffer.append(line);
        }
        br.close();
        //XML转为JAVA对象
        ReaderXml cityList = (ReaderXml) XmlBuilder.xmlStrToObject(ReaderXml.class, buffer.toString());
        return cityList.getXmlLst();
    }
}